package tmproject.hlhj.fhp.tmvote.module

import android.content.Context
import com.example.mymvp.BaseModule
import com.lzy.okgo.model.Response
import tmproject.hlhj.fhp.tmvote.beans.*
import tmproject.hlhj.fhp.tmvote.network.Apis
import tmproject.hlhj.fhp.tmvote.network.BaseObersever
import tmproject.hlhj.fhp.tmvote.network.LoadingAction

/**
 * Created by Never Fear   on 2018\8\6 0006.
Never More....
 */
class VotingModule :BaseModule {
    var listener:OnVotingResp?=null
    /*获取详情数据*/
    fun getVotingData(id:Int,appStr:String,context: Context){
        Apis.getVoteDetail(id,appStr)
                .subscribe(object :BaseObersever<Response<VotingBean>>(context){
                    override fun onRespSuccess(t: Response<VotingBean>) {
                        val votingBean = t.body()
                        listener?.onVotingResp(votingBean)
                    }
                })
    }

    /*获取选手列表*/
    fun getPlayerList(id:Int,page:Int,pageSize:Int,context: Context){
        Apis.getPlayerList(id,page,pageSize)
                .subscribe(object :BaseObersever<Response<PlayerListBean>>(context){
                    override fun onRespSuccess(t: Response<PlayerListBean>) {
                        val playerListBean = t.body()
                        listener?.onPlayerListBean(playerListBean)
                    }
                })
    }

    /*投票*/
    fun vote(competitor_id:String,user_id:String,context: Context){
        Apis.vote(competitor_id, user_id)?.let {
            it.doOnSubscribe(LoadingAction(context))

                    .subscribe(object :BaseObersever<Response<BaseBean>>(context){
                        override fun onRespSuccess(t: Response<BaseBean>) {
                            val baseBean = t.body()
                            listener?.onVoteResp(baseBean)
                        }

                    })
        }
    }

    /*分享链接获取*/
    fun getShareUrl(id:Int,context: Context){
        Apis.getShare(id)
                .doOnSubscribe(LoadingAction(context))
                .subscribe(object :BaseObersever<Response<ShareBean>>(context){
                    override fun onRespSuccess(t: Response<ShareBean>) {
                        listener?.onShareUrlResp(t.body())
                    }
                })
    }
    /*搜索*/
    fun search(activity_id:String,name:String,context: Context){
        Apis.search(activity_id, name)?.let {
            it.doOnSubscribe(LoadingAction(context))
                    .subscribe(object :BaseObersever<Response<SearchBean>>(context){
                        override fun onRespSuccess(t: Response<SearchBean>) {
                            val searchBean = t.body()
                            listener?.onSearchResp(searchBean)
                        }
                    })
        }
    }
    interface OnVotingResp{
        fun onVotingResp(votingBean: VotingBean)

        fun onPlayerListBean(playerListBean: PlayerListBean)

        fun onVoteResp(baseBean: BaseBean)

        fun onSearchResp(searchBean: SearchBean)

        fun onShareUrlResp(shareBean: ShareBean)
    }
}